/*
## 2024/01/22 # 黑店小小二 # 关闭自动任命官员的行动力技巧要求 ##
## 2023/11/02 # 黑店小小二 # 修复玩家也触发自动设定内政人员 ##
## 2023/10/09 # 黑店小小二 # 去除注释代码 ##
## 2023/07/26 # 黑店小小二 # 修复筑城开支设定取错字段 ##
## 2023/07/25 # 黑店小小二 # 自动任命官员及自动设定开支逻辑处理移动到此；calculate_building2增加update_data参数等 ##
## 2023/05/11 # 江东新风 # 仅在执政官功能开启时显示和执行 ##
## 2023/5/10 # 江东新风 # 收益计算函数重写
## 2023/1/19 # 铃 # 修复数个bug
## 2022/12/3 # 铃 # 修复数个bug
## 2022/8/8 # 铃 # 全面自动化内政包括以下功能
新增功能:

a.匹配20220328版本的人口系统,所有的生产能力.招兵基准.财务收支都和人口密切挂钩.城防
b.匹配20220328版本的伤兵系统,新增回归兵系统
c.基于San14的挂机流玩法,所有招兵.训练.生产.造船等均为自动进行.
d.新增额外城防系统,可以通过不断的发展提高额外城防,在受到攻击时,优先扣除额外城防

设定细节:
1.计算人口:人口拥有增长率,增长率和治安.前线与否.战斗与否.城防.设施数量等等挂钩.
2.招兵自动化算法:每旬根据城市里人物属性加成,自动获得潜在兵役的一部分比例
3.兵装生产自动化算法:每旬根据城市里人物属性加成,自动获得部分兵装,特殊城市还有特殊加成.前线与否
4.伤兵恢复系统:城市和部队在作战中会产生伤兵,伤兵回归城市后会计算为城市伤兵,城市会缓慢恢复伤兵,部队溃散也会有一小部分伤兵成为回归伤兵.缓慢回归城市
5.额外城防可以增加城市内部队防御力,可以影响城市人口增量.
*/

namespace 内政自动
{

	// ================ CUSTOMIZE ================
	const int RANK_TP_COST = 50;	  // 自动任命官员技巧要求
	const int RANK_ACTION_COST = 100; // 自动任命官员行动力要求

	const int KOUSEKI_GAIN = 40; // 原版执行对应内政，功绩是50
	const int TP_GAIN = 6;		 // 原版执行对应内政，最大收益是10
	const float tokusan_sale = 1.5f;
	const bool 调试模式 = false;
	// const float tokusan_sale = float(pk::core["tokusan.sale_rate"]);

	class Main
	{

		pk::building @building_;
		pk::force @force_;
		pk::person @taishu_;

		Main()
		{
			pk::bind(102, pk::trigger102_t(StartSet), 1);
			// pk::bind(107, pk::trigger107_t(callback));
			pk::bind(112, pk::trigger112_t(onTurnEnd));
		}

		// 开局时设定基础数据
		void StartSet()
		{
			if (!pk::get_scenario().loaded)
			{
			}
			else
			{
				// 因为读档时城市收入不会更新，而数据计算又需要收入支持，所以加入此函数
				for (int city_id = 0; city_id < 城市_末; city_id += 1)
				{
					auto base_t = ch::get_base_p(city_id);
					pk::city @city = pk::get_city(city_id);
					CITY_REVENUE::get_revenue(city);
					CITY_HARVEST::get_harvest(city);
					// base_t.base_revenue = CITY_REVENUE::get_revenue(city); // cast<pk::func150_t>(pk::get_func(150))(city);
					// base_t.base_harvest = CITY_HARVEST::get_harvest(city); // cast<pk::func151_t>(pk::get_func(151))(city);
				}

				// for (int building_id = 0; building_id < 据点_末; building_id += 1)
				// {
				// 	// 开局设定兵役政策为2;
				// 	auto building_p = ch::get_baseIA(building_id);
				// 	building_p.building_policy = 2;
				// }

				// 剧本读取时需要获取所有城市的
				// pk::building @building = pk::get_building(城市_邺);
				// set_auto_IA_order(building);
				// pk::trace("set_auto_IA_order 1 城市_邺" + ch::get_baseIA(城市_邺).troops_effic);
			}
		}

		void callback2()
		{
			if (!ch::get_auto_affairs_status())
				return;
			// 信息显示_顶部面板();//干掉，现要求信息显示全写在400文件
		}

		void onTurnEnd(pk::force @force)
		{
			if (pk::is_alive(force))
			{
				//此处可以重置该势力的autoafairbase 和role
				reset_autoaffair(force);

				for (int i = 0; i < 据点_末; i += 1)
				{
					pk::building @building = pk::get_building(i);

					if (building !is null and building.get_force_id() == force.get_id())
					{
						if (!ch::get_auto_affairs_status(building))
							continue;
						set_auto_IA_order(building);
						auto building_p = ch::get_baseIA(building.get_id());
						pk::person @士兵 = pk::get_person(武将_士兵);

						int 总开支 = int((building_p.troops_effic + building_p.repair_effic + building_p.porder_effic + building_p.train_effic + building_p.weapon_effic + building_p.horse_effic + building_p.punch_effic + building_p.boat_effic));

						if (building.is_player() and 总开支 > pk::get_gold(building))
						{
							pk::message_box(pk::encode(pk::format("报告!\x1b[2x{}\x1b[0x资金不足,本回合自动内政无效.现有资金\x1b[2x{}\x1b[0x.所需资金\x1b[2x{}", pk::decode(pk::get_name(building)), pk::get_gold(building), 总开支)), 士兵);
							return;
						}

						calculate_building2(pk::get_building(i));
					}
				}
			}
		}

		void reset_autoaffair(pk::force @force)
		{
			if (force.is_player()) return;//不处理玩家势力
			int force_id = force.get_id();
			//只处理ai吧
			for (int i = 0; i < 非贼武将_末; ++i)
			{
				auto person = pk::get_person(i);
				if (!pk::is_alive(person)) continue;
				if (person.get_force_id() != force_id) continue;
				auto person_p = ch::get_person_p(i);
				person_p.auto_affairs_base = 255;
				person_p.auto_affairs_role = 255;
			}
		}
	} // class Main

	Main main;

	// update_data - 数值变更后，是否立刻更新城池资源属性
	void calculate_building2(pk::building @base, bool update_data = true)
	{
		// 还得考虑开支的影响以及是否在城中
		int force_id = base.get_force_id();
		if (!pk::is_valid_force_id(force_id))
			return;

		// 征兵
		int troops_gain = calc_troops_gain(base, update_data);
		// 治安
		int porder_gain = calc_porder_gain(base, update_data);
		// 训练
		int train_gain = calc_train_gain(base, update_data);
		// 生产
		int weapon_gain = calc_weapon_gain(base, update_data);
		// 育马
		int horse_gain = calc_horse_gain(base, update_data);
		// 攻具
		int punch_gain = calc_punch_gain(base, update_data);
		// 船舰
		int boat_gain = calc_boat_gain(base, update_data);
		// 城防
		int repair_gain = calc_repair_gain(base, update_data);

		// 总和
		int total_gain = troops_gain + porder_gain + train_gain + weapon_gain + horse_gain + punch_gain + boat_gain + repair_gain;
		pk::add_gold(base, -total_gain, true);
	}

	void set_auto_IA_order(pk::building @building)
	{
		if (building.get_force_id() == -1)
			return;
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());
		pk::district @district = pk::get_district(building.get_district_id());

		// AI开支设定
		if ((pk::get_taishu_id(building) != -1) and (!pk::is_player_controlled(building)) and building.get_id() >= 0 and building.get_id() < 87)
		{

			int person_count = pk::get_person_list(building, pk::mibun_flags(身份_君主, 身份_都督, 身份_太守, 身份_一般)).count;

			int 空地数量 = 0;
			if (building.get_id() >= 建筑_城市开始 and building.get_id() < 建筑_城市末)
			{
				pk::city @city = pk::building_to_city(building);
				空地数量 = city.max_devs - city.num_devs;
			}

			// 更换算法,默认投入总收入的80%和现有资金的8%.这样不容易出现摆动.
			// 内政开支计算要考虑军饷维护费和兵装维护费

			// 由于扣钱变成了回合末扣钱,而收入却是回合初收钱,因此需要重新考虑算法
			// 收入是月度的,支出是旬度的,因此不能太机械的直接计算,要考虑到资金不足的情况下.
			// 由于府收支的存在,因此可以多花点钱
			
			int 总分配资金 = int((base_t.base_revenue / 3 - building_p.troops_expense - building_p.weapon_expense) * 0.85) + int(pk::min(pk::max(0, pk::get_gold(building) - 3000), 30000) * 0.2);
			总分配资金 = int(总分配资金 * (空地数量 > 3 ? 0.3 : 1));
			int 最大开支 = 0;

			// 月初要考虑到今天的钱必须足够三次扣减,而且需要留下部分保底发工资
			//  列方程 (x+a+b)*3 < gold- 300 →x<gold/3-a-b - 100
			if (pk::get_day() == 1) // 月初
			{
				最大开支 = pk::get_gold(building) / 3 - building_p.troops_expense - building_p.weapon_expense - 0;
				总分配资金 = pk::clamp(总分配资金, 0, 最大开支); // 防止溢出,最大值为5000.
			}

			// 月末扣完钱之后,下个月初就会到账,因此只要不把钱花光就行,留下200元以防万一需要;
			if (pk::get_day() == 11) // 月中
			{
				最大开支 = pk::get_gold(building) / 2 - building_p.troops_expense - building_p.weapon_expense - 100;
				总分配资金 = pk::clamp(总分配资金, 0, 最大开支); // 防止溢出,最大值为5000.
			}

			// 月中要考虑到
			if (pk::get_day() == 21) // 月底
			{
				最大开支 = pk::get_gold(building) - building_p.troops_expense - building_p.weapon_expense - 200;
				总分配资金 = pk::clamp(总分配资金, 0, 最大开支); // 防止溢出,最大值为5000.
			}

			//  if (building.get_id() == 铃据点_天水)
			// 	pk::trace(pk::format("城市资金：{},总分配资金：{},最大开支：{},军饷：{},维护费：{}", pk::get_gold(building),总分配资金,最大开支,building_p.troops_expense,building_p.troops_expense));

			// 军团指令 资金重视
			if (building.is_player())
			{
				if (!pk::is_player_controlled(district) and district.conserve_gold)
					总分配资金 = 总分配资金 * 20 / 100;
				if (!pk::is_player_controlled(district) and !district.conserve_gold)
					总分配资金 = 总分配资金 * 100 / 100;
			}

			// 每次重置
			building_p.repair_effic = 0;
			building_p.porder_effic = 0;
			building_p.train_effic = 0;
			building_p.troops_effic = 0;
			building_p.weapon_effic = 0;
			building_p.horse_effic = 0;
			building_p.punch_effic = 0;
			building_p.boat_effic = 0;

			int building_id = building.get_id();
			int 总分 = 0;
			int 项目数量 = 0;

			// 项目列表二元数组
			array<array<int>> order_list = {
				{内政自动化_招兵, 0},
				{内政自动化_训练, 0},
				{内政自动化_巡查, 0},
				{内政自动化_兵装, 0},
				{内政自动化_育马, 0},
				{内政自动化_攻具, 0},
				{内政自动化_造船, 0} };

			for (int i = 0; i < 7; i++)
			{
				order_list[i][1] = AI内政打分(building, order_list[i][0], 0);

				总分 += order_list[i][1];
				if (order_list[i][1] > 0)
					项目数量++;
			// 	  if (building.get_id() == 铃据点_天水)
			// 	 	pk::trace(pk::format("初次打分后,项目：{},项目分:{}", order_list[i][0], order_list[i][1]));
			 }

			order_list.sort(function(a, b) {
				return a[1] > b[1];
			});

			for (int i = 0; i < 7; i++)
			{
				building_p.rank_type[i] = order_list[i][0];
			}

			// 排序后任命一次
			AI内政官任命(building);

			// 改回初始顺序
			order_list.sort(function(a, b) {
				return a[0] < b[0];
			});

			// 项目列表最大开支
			array<array<int>> order_list_max = {
				{内政自动化_招兵, 0},
				{内政自动化_训练, 0},
				{内政自动化_巡查, 0},
				{内政自动化_兵装, 0},
				{内政自动化_育马, 0},
				{内政自动化_攻具, 0},
				{内政自动化_造船, 0} };

			int 总最大开支 = 0;
			for (int i = 0; i < 7; i++)
			{
				order_list_max[i][1] = 开支级别(building, order_list_max[i][0]);
				总最大开支 += 开支级别(building, order_list_max[i][0]);
			}

			// 再重新分配打分
			for (int i = 0; i < 7; i++)
			{
				order_list[i][1] = AI内政打分(building, order_list[i][0], 1) * 开支级别(building, order_list_max[i][0]) / 100;

				总分 += order_list[i][1];
				if (order_list[i][1] > 0)
					项目数量++;

			//      if (building.get_id() == 铃据点_天水)
			// 	 	pk::trace(pk::format("项目：{},项目分:{},项目最大:{}", order_list[i][0], order_list[i][1], 开支级别(building, order_list_max[i][0])));
			 }

			// 筑城分也要在总分里面
			总分 += AI筑城设置(building);

			if (总分 > 0 and 总分配资金 > 0)
			{
				building_p.troops_effic = pk::min(order_list_max[0][1], int(float(order_list[0][1]) / float(总分) * 总分配资金));
				building_p.train_effic = pk::min(order_list_max[1][1], int(float(order_list[1][1]) / float(总分) * 总分配资金));
				building_p.porder_effic = pk::min(order_list_max[2][1], int(float(order_list[2][1]) / float(总分) * 总分配资金));
				building_p.weapon_effic = pk::min(order_list_max[3][1], int(float(order_list[3][1]) / float(总分) * 总分配资金));
				building_p.horse_effic = pk::min(order_list_max[4][1], int(float(order_list[4][1]) / float(总分) * 总分配资金));
				building_p.punch_effic = pk::min(order_list_max[5][1], int(float(order_list[5][1]) / float(总分) * 总分配资金));
				building_p.boat_effic = pk::min(order_list_max[6][1], int(float(order_list[6][1]) / float(总分) * 总分配资金));
				building_p.repair_effic = pk::min(开支级别(building, 内政自动化_筑城), int(float(AI筑城设置(building)) / float(总分) * 总分配资金));

				//  if (building.get_id() == 铃据点_零陵)
				//  pk::trace(pk::format("城市id：{},城市名:{},building_p.repair_effic:{}", building.get_id(), pk::decode(pk::get_name(building)), building_p.repair_effic));

				// if (building.get_id() == 铃据点_平原)
				// 	pk::trace(pk::format("项目：{},项目分:{}", "巡查最大值", float(order_list_max[2][1])));
			}
		}
	}

	int AI内政打分(pk::building @building, int 项目, int mode /* 0为计算,1为分配 */)
	{
		int 分数 = 0;
		switch (项目)
		{
			case 内政自动化_招兵:
				分数 = AI招兵设置(building, mode); // 重视兵力
				break;
			case 内政自动化_训练:
				分数 = AI训练设置(building, mode);
				break;
			case 内政自动化_巡查:
				分数 = AI巡查设置(building, mode);
				break;
			case 内政自动化_兵装:
				分数 = AI兵装设置(building, mode);
				break;
			case 内政自动化_育马:
				分数 = AI育马设置(building, mode);
				break;
			case 内政自动化_攻具:
				分数 = AI攻具设置(building, mode);
				break;
			case 内政自动化_造船:
				分数 = AI造船设置(building, mode);
				break;
			default:
				break;
		}
		// if (building.get_id() == 铃据点_天水)
		//  pk::trace("项目" + 项目 + ",mode " + mode);
		return 分数;
	}

	int AI招兵设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 200;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());
		if (pk::enemies_around(building))
			return 0;

		if (!pk::is_valid_person_id(building_p.recruit_person) and mode == 1)
			return 0;

		//  if (building.get_id() == 铃据点_天水)
		//  	pk::trace(pk::format("1mode：{},ratio:{}", mode, ratio));

		int 总兵装含战马 = pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3) + pk::get_weapon_amount(building, 4);
		int 兵装富余 = 总兵装含战马 - pk::get_troops(building);
		int 理想兵力 = int(base_t.population / 12) + 20000;													// 考虑到出征的可能,大约100万的城市可以养活6万兵力.
		float 粮草兵力比 = pk::max(1000, pk::get_food(building)) / pk::max(1000, pk::get_troops(building)); // 兵粮不足的时候要降低招兵开支
		int 粮草富余 = pk::get_food(building) - pk::get_troops(building);									// 计算之后大约为0.87倍,简化为1倍

		// 每1万兵力需要12万人口养活,因此距离理想兵力越少,开支越高.每差1万人,多20分.
		ratio += (理想兵力 - pk::get_troops(building)) * 20 / 10000;

		// 粮草分,减分项,随着粮草富余变化而变化,每10000差值 20分
		if (粮草富余 < 0)
			ratio += 粮草富余 * 20 / 10000;

		// 兵役人口分,减分项,随着兵役人口的变化而变化,每多1万兵役加100分,每少1万兵役扣20分
		if ( base_t.mil_pop_all > 10000)
			ratio += pk::min(100, int(( base_t.mil_pop_all - 10000) * 100 / 10000));
		else
			ratio -= pk::max(100, int((base_t.mil_pop_all - 10000) * 20 / 10000));

		// 兵装分,每1万兵装要求有1万兵力,兵装越过剩,越征兵,每多1万兵装加50分,每少1万兵装扣20分
		if (兵装富余 > 0)
			ratio += (总兵装含战马 - pk::get_troops(building)) * 50 / 10000;
		else
			ratio += (总兵装含战马 - pk::get_troops(building)) * 20 / 10000;

		pk::city @city = pk::building_to_city(building);

		// 判断有无设施,征兵可用次数大于0，既有兵营
		bool has_兵营 = city is null ? false : city.barracks_counter > 0;
		// if (building.get_id() < 42) // 城市判定
		// {
		// 	for (int i = 0; i < city.max_devs; i++)
		// 	{
		// 		// 为了避免遍历城市周围地格
		// 		pk::building @t_building = city.dev[i].building;
		// 		if (pk::is_alive(t_building) && (t_building.facility == 设施_兵营1级 or t_building.facility == 设施_兵营2级 or t_building.facility == 设施_兵营3级) && t_building.completed)
		// 		{
		// 			has_兵营 = true;
		// 			break;
		// 		}
		// 	}
		// }

		// 有名声加50分.
		if (pk::is_valid_person_id(building_p.recruit_person))
		{
			if (ch::has_skill(pk::get_person(building_p.recruit_person), 特技_名声))
				ratio += 50;
		}

		if (building.get_id() >= 城市_末) // 小城由于兵役经常过剩,增加征兵开支
			ratio += 500;

		// if (building.get_id() == 铃据点_乐浪)
		// 	pk::trace(pk::format("2mode：{},ratio:{}", mode, ratio));

		// 玩家的军团命令
		pk::district @district = pk::get_district(building.get_district_id());
		if (building.is_player() and !pk::is_player_controlled(district) and district.gather_troops)
			ratio = int(ratio * 1.2);

		if (building.is_player() and !pk::is_player_controlled(district) and !district.gather_troops)
			ratio = int(ratio * 0.3);

		// 治安太低不征兵或者少征兵
		int order = 0;
		if (building_id >= 据点_城市末 and building_id < 据点_末)
			order = ch::get_base_p(building_id).public_order;
		if (building_id >= 0 and building_id < 据点_城市末)
			order = pk::building_to_city(building).public_order;

		if (order < 80)
			ratio = 0;
		else if (order < 85)
			ratio = int(ratio * 0.2);
		else if (order < 90)
			ratio = int(ratio * 0.3);
		else if (order < 95)
			ratio = int(ratio * 0.6);

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("招兵ratio:{},", ratio));
		// 城市无兵营则放弃
		if (!has_兵营 and building.get_id() < 42)
			ratio = 0;
		ratio = pk::clamp(ratio, 0, 500); // 防止溢出,最大值为500.

		// if (building.get_id() == 铃据点_乐浪)
		// 	pk::trace(pk::format("3mode：{},ratio:{}", mode, ratio));

		// 满兵或者无兵役人口.或无官员则不分配
		if (base_t.mil_pop_all > 2000 and building.troops < pk::get_max_troops(building))
		{
			
			// if (building.get_id() == 铃据点_乐浪)
			// 	pk::trace(pk::format("4mode：{},ratio:{}", mode, ratio));
			return ratio;
		}
		else
			return 0;
	}

	int AI训练设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 100;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		if (!pk::is_valid_person_id(building_p.drill_person) and mode == 1)
			return 0;
		int last_energy = pk::get_max_energy(building) - building.energy;
		if (last_energy > 30)
			ratio = 3000;
		else if (last_energy > 20)
			ratio = 2000;
		else if (last_energy > 10)
			ratio = 1000;
		else if (last_energy > 5)
			ratio = 500;
		else
			ratio = 30;

		ratio += AI招兵设置(building, 1) / 5;

		ratio = pk::clamp(ratio, 0, 3000); // 防止溢出,最大值为500.

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("训练ratio:{},", ratio));

		if (building.energy != pk::get_max_energy(building) or AI招兵设置(building, 0) > 50)
			return ratio;
		else
			return 0;
	}

	int AI巡查设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 100;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		if (!pk::is_valid_person_id(building_p.inspections_person) and mode == 1)
			return 0;

		int order = 0;
		if (building_id >= 据点_城市末 and building_id < 据点_末)
			order = ch::get_base_p(building_id).public_order;
		if (building_id >= 0 and building_id < 据点_城市末)
			order = pk::building_to_city(building).public_order;

		// if (building.get_id() == 铃据点_犍为)
		// 	pk::trace(pk::format("巡查ratio:{},order{},", ratio,order));

		if (order < 70)
			ratio = 3000;
		if (order < 80)
			ratio = 2000;
		else if (order < 90)
			ratio = 1000;
		else if (order < 95)
			ratio = 300;
		else
			ratio = 100;

		ratio += AI招兵设置(building, 1) / 5;

		// if (building.get_id() == 铃据点_犍为)
		// 	pk::trace(pk::format("巡查ratio:{},order{},", ratio,order));

		ratio = pk::clamp(ratio, 0, 3000); // 防止溢出,最大值为3000.

		// if (building.get_id() == 铃据点_犍为)
		// 	pk::trace(pk::format("巡查ratio:{},order{},", ratio,order));

		if (order <= 99 or AI招兵设置(building, 0) > 50)
			return ratio;
		else
			return 0;
	}
	int AI兵装设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 200;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		if (!pk::is_valid_person_id(building_p.weapon_person) and mode == 1)
			return 0;
		if (pk::enemies_around(building))
			return 0;

		// 马的数量理论上也应该影响本分数,如果马很多的话就不需要那么多兵装了
		int 总兵装 = pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3);
		int 兵装最大值 = pk::get_max_weapon_amount(building, 1) + pk::get_max_weapon_amount(building, 2) + pk::get_max_weapon_amount(building, 3);
		int 总兵装含战马 = pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3) + pk::get_weapon_amount(building, 4);
		int 兵装富余 = 总兵装含战马 - pk::get_troops(building);
		int 枪数量 = pk::get_weapon_amount(building, 1);
		int 戟数量 = pk::get_weapon_amount(building, 2);
		int 弩数量 = pk::get_weapon_amount(building, 3);
		int 枪最大数量 = pk::get_max_weapon_amount(building, 1);
		int 戟最大数量 = pk::get_max_weapon_amount(building, 2);
		int 弩最大数量 = pk::get_max_weapon_amount(building, 3);

		// 兵装分,每1万兵装要求有1万兵力,兵装越缺越生产,每少1万兵装加20分,每少1万兵装扣5分
		if (兵装富余 > 0)
			ratio -= 兵装富余 * 5 / 10000;
		else
			ratio -= 兵装富余 * 20 / 10000;

		pk::city @city = pk::building_to_city(building);
		// 判断有无设施
		bool has_锻冶 = city is null ? false : city.blacksmith_counter > 0;
		// blacksmith_counter
		// if (building.get_id() < 42) // 城市判定
		// {
			
		// 	for (int i = 0; i < city.max_devs; i++)
		// 	{
		// 		pk::building @t_building = city.dev[i].building;
		// 		if (pk::is_alive(t_building) && (t_building.facility == 设施_锻冶1级 or t_building.facility == 设施_锻冶2级 or t_building.facility == 设施_锻冶3级) && t_building.completed)
		// 		{
		// 			has_锻冶 = true;
		// 			break;
		// 		}
		// 	}
		// }

		if (pk::is_valid_person_id(building_p.weapon_person))
		{
			if (ch::has_skill(pk::get_person(building_p.weapon_person), 特技_能吏))
				ratio += 50;
		}

		// 如果空挡太多,则增加兵装生产的开支.
		if (AI攻具设置(building, 0) == 0)
			ratio += 50;
		if (AI造船设置(building, 0) == 0)
			ratio += 50;
		if (AI育马设置(building, 0) == 0)
			ratio += 50;

		bool 特色_枪 = false;
		bool 特色_戟 = false;
		bool 特色_弩 = false;
		if (building.get_id() < 42) // 城市判定
		{
			// pk::city @city = pk::building_to_city(building);
			特色_枪 = city.tokusan[pk::equipment_id_to_heishu(兵器_枪)];
			特色_戟 = city.tokusan[pk::equipment_id_to_heishu(兵器_戟)];
			特色_弩 = city.tokusan[pk::equipment_id_to_heishu(兵器_弩)];
		}

		// 补短板,在总兵装不低,但是某一项特别低的时候
		// 戟不能太低,否则AI会出动C骑兵.
		// 为不同的特色城市设定逻辑

		if (特色_枪 and 戟数量 > pk::max(15000, int(building.troops * 0.3)) and 弩数量 > pk::max(15000, int(building.troops * 0.3)) and 枪数量 - int(枪最大数量 * 0.9) < 0)
			building_p.weapon_choose = 1;

		else if (特色_戟 and 枪数量 > pk::max(15000, int(building.troops * 0.3)) and 弩数量 > pk::max(15000, int(building.troops * 0.3)) and 戟数量 - int(戟最大数量 * 0.9) < 0)
			building_p.weapon_choose = 2;

		else if (特色_弩 and 枪数量 > pk::max(15000, int(building.troops * 0.3)) and 戟数量 > pk::max(15000, int(building.troops * 0.3)) and 弩数量 - int(弩最大数量 * 0.9) < 0)
			building_p.weapon_choose = 3;

		// 由于AI文官多且戟兵更擅长AI,因此AI对戟兵优先级很高.只要有基础的其他兵装用于高适性武将就可以开始产戟

		else if (!特色_枪 and !特色_戟 and !特色_弩 and 枪数量 > pk::max(25000, int(building.troops * 0.5)) and 弩数量 > pk::max(25000, int(building.troops * 0.5)) and 戟数量 - int(戟最大数量 * 0.9) < 0)
			building_p.weapon_choose = 2;
		else
			building_p.weapon_choose = 0;

		// 玩家的军团命令
		pk::district @district = pk::get_district(building.get_district_id());
		if (building.is_player() and !pk::is_player_controlled(district) and (!district.produce_bows and !district.produce_yari and !district.produce_geki))
			ratio = int(ratio * 0.3);

		if (district.produce_yari and !district.produce_bows and !district.produce_geki)
			building_p.weapon_choose = 1;
		if (!district.produce_yari and !district.produce_bows and district.produce_geki)
			building_p.weapon_choose = 2;
		if (!district.produce_yari and district.produce_bows and !district.produce_geki)
			building_p.weapon_choose = 3;

		if (!has_锻冶 and building.get_id() < 42)
			ratio = 0;

		ratio = pk::clamp(ratio, 0, 500); // 防止溢出,最大值为500.

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("兵装ratio:{},", ratio));

		if (总兵装 < int(兵装最大值 * 0.99))
			return ratio;
		else
			return 0;
	}

	int AI育马设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 100;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		if (!pk::is_valid_person_id(building_p.horse_person) and mode == 1)
			return 0;
		if (pk::enemies_around(building))
			return 0;

		int 总战马 = pk::get_weapon_amount(building, 4);
		int 总兵装含战马 = pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3) + pk::get_weapon_amount(building, 4);
		int 兵装富余 = 总兵装含战马 - pk::get_troops(building);
		bool 特色_战马 = false;

		pk::city @city = pk::building_to_city(building);
		if (building.get_id() < 42) // 城市判定
		{
			特色_战马 = city.tokusan[pk::equipment_id_to_heishu(兵器_战马)];
		}
		// 考虑到存在无兵装的兵力来源,以及零头无法被利用,
		// 特产城市中,每1万兵大约需要8000战马,
		// 非特产城市中,每1万兵大约需要3000战马,

		// 兵装分,每1万兵装要求有1万兵力,兵装越缺越生产,每少1万兵装加20分,每少1万兵装扣5分
		if (兵装富余 > 0)
			ratio -= 兵装富余 * 5 / 10000;
		else
			ratio -= 兵装富余 * (特色_战马 ? 30 : 10) / 10000;

		// 判断有无设施
		bool has_厩舍 = city is null ? false : city.stable_counter > 0;
		// if (building.get_id() < 42) // 城市判定
		// {
		// 	for (int i = 0; i < city.max_devs; i++)
		// 	{
		// 		// 为了避免遍历城市周围地格
		// 		pk::building @t_building = city.dev[i].building;
		// 		if (pk::is_alive(t_building) && (t_building.facility == 设施_厩舍1级 or t_building.facility == 设施_厩舍2级 or t_building.facility == 设施_厩舍3级) && t_building.completed)
		// 		{
		// 			has_厩舍 = true;
		// 			break;
		// 		}
		// 	}
		// }

		if (has_厩舍 and 特色_战马)
			ratio += 50;

		if (pk::is_valid_person_id(building_p.horse_person))
		{
			if (ch::has_skill(pk::get_person(building_p.horse_person), 特技_繁殖))
				ratio += 100;
		}
		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("育马ratio:{},", ratio));

		// 小城能做的事儿太少,要加分.
		if (building.get_id() >= 城市_末)
			ratio += 100;

		// 玩家的军团命令
		pk::district @district = pk::get_district(building.get_district_id());
		if (building.is_player() and !pk::is_player_controlled(district) and district.produce_horses)
			ratio = int(ratio * 1.2);

		if (building.is_player() and !pk::is_player_controlled(district) and !district.produce_horses)
			ratio = int(ratio * 0.3);

		if (!has_厩舍 and building.get_id() < 42)
			ratio = 0;

		ratio = pk::clamp(ratio, 0, 500); // 防止溢出,最大值为500.

		if (pk::get_weapon_amount(building, 4) < pk::get_max_weapon_amount(building, 4))
			return ratio;
		else
			return 0;
	}
	int AI攻具设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 50;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		// 小城市不造攻具
		if (pk::enemies_around(building) or building.get_id() >= 城市_末)
			return 0;

		if (!pk::is_valid_person_id(building_p.punch_person) and mode == 1)
			return 0;

		bool 特色_冲车 = false;
		pk::city @city = pk::building_to_city(building);
		if (building.get_id() < 42) // 城市判定
		{
			特色_冲车 = city.tokusan[pk::equipment_id_to_heishu(兵器_冲车)];
		}

		// 判断有无设施
		bool has_工房 = city is null ? false : city.	workshop_counter > 0;
		// if (building.get_id() < 42) // 城市判定
		// {
		// 	pk::city @city = pk::building_to_city(building);
		// 	for (int i = 0; i < city.max_devs; i++)
		// 	{
		// 		// 为了避免遍历城市周围地格
		// 		pk::building @t_building = city.dev[i].building;
		// 		if (pk::is_alive(t_building) && t_building.facility == 设施_工房 && t_building.completed)
		// 		{
		// 			has_工房 = true;
		// 			break;
		// 		}
		// 	}
		// }

		int 总兵装含战马 = pk::get_weapon_amount(building, 1) + pk::get_weapon_amount(building, 2) + pk::get_weapon_amount(building, 3) + pk::get_weapon_amount(building, 4);
		int 总攻具 = pk::get_weapon_amount(building, 5) + pk::get_weapon_amount(building, 6) + pk::get_weapon_amount(building, 7) + pk::get_weapon_amount(building, 8);
		int 所需配置 = pk::get_troops(building) / 8000;

		// 因为特色城市可以卖钱,所以只要有钱就可以产
		if (特色_冲车)
		{
			// 1万块钱30分
			ratio += int(pk::get_gold(building) / 10000) * 30;
		}
		else
		{

			// 钱越多越产,1万块钱20分
			ratio += int(pk::get_gold(building) / 10000) * 20;

			// 越过剩越不产,超过30个左右就几乎不产了
			if (所需配置 > 总攻具)

				ratio += (所需配置 - 总攻具) * 10;
			else
				ratio += (所需配置 - 总攻具) * 5;
		}

		if (pk::is_valid_person_id(building_p.punch_person))
		{
			if (ch::has_skill(pk::get_person(building_p.punch_person), 特技_发明))
				ratio += 50;
		}

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("攻具ratio:{},兵力:{}", ratio,pk::get_troops(building)));

		// 玩家的军团命令
		pk::district @district = pk::get_district(building.get_district_id());
		if (building.is_player() and !pk::is_player_controlled(district) and district.produce_heiki)
			ratio = int(ratio * 1.2);

		if (building.is_player() and !pk::is_player_controlled(district) and !district.produce_heiki)
			ratio = int(ratio * 0.3);

		if (!has_工房 and building.get_id() < 42)
			ratio = 0;

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("攻具ratio:{},", ratio));

		ratio = pk::clamp(ratio, 0, 500); // 防止溢出,最大值为500.
		return ratio;
	}
	int AI造船设置(pk::building @building, int mode /* 0为计算,1为分配 */)
	{
		int ratio = 0;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building_id);
		auto building_p = ch::get_baseIA(building_id);

		// 小城市不造船
		if (pk::enemies_around(building) or (building.get_id() >= 城市_末 and building.get_id() != 铃据点_乐浪))
			return 0;

		if (!pk::is_valid_person_id(building_p.boat_person) and mode == 1)
			return 0;

		pk::city @city = pk::building_to_city(building);
		// 判断有无设施
		bool has_造船 = city is null ? false : city.shipyard_counter > 0;
		// if (building.get_id() < 42) // 城市判定
		// {
		// 	for (int i = 0; i < city.max_devs; i++)
		// 	{
		// 		pk::building @t_building = city.dev[i].building;
		// 		if (pk::is_alive(t_building) && t_building.facility == 设施_造船 && t_building.completed)
		// 		{
		// 			has_造船 = true;
		// 			break;
		// 		}
		// 	}
		// }

		// 没有设施就不造船,大小城市都一样
		if (!has_造船)
			return 0;

		bool 兵种_水军 = false;
		if (building.get_id() < 42) // 城市判定
		{
			兵种_水军 = city.tokusan[pk::equipment_id_to_heishu(兵器_斗舰)];
		}

		// 计算海上敌对势力
		int route_type = -1;
		int sea_enemy_count = 0;
		for (int i = 0; i < 6; i++)
		{
			int neighbor_base_id = pk::get_neighbor_area_building(building.get_id(), i);
			if (neighbor_base_id >= 0 and neighbor_base_id <= 据点_末)
			{
				route_type = pk::get_neighbor_area_route(building.get_id(), i);

				if (pk::is_enemy(building, pk::get_building(neighbor_base_id)) and route_type == 路径_海洋)
					sea_enemy_count++;
			}
		}

		int 总船只 = pk::get_weapon_amount(building, 10) + pk::get_weapon_amount(building, 11);
		int 理想船只 = pk::get_troops(building) / 5000 + 5;
		bool 造船_特技 = false;

		if (pk::is_valid_person_id(building_p.boat_person))
		{
			if (ch::has_skill(pk::get_person(building_p.boat_person), 特技_造船))
				造船_特技 = true;
		}

		// 因为特色城市可以卖钱,所以只要有钱就可以产
		if (兵种_水军)
		{
			ratio += 造船_特技 ? 100 : 50;
			// 1越有钱越造1万块钱20分,特技30分
			ratio += int(pk::get_gold(building) / 10000) * (造船_特技 ? 30 : 10);
			// 每一个相邻地方水军据点都加50分
			ratio += sea_enemy_count * 50;
		}

		// 对于没有特产的城市来说,只有很有钱的时候,或者临近有敌军海上势力时,才会开支
		else
		{
			ratio += 造船_特技 ? 50 : 0;
			// 1越有钱越造1万块钱10分,特技30分
			ratio += int(pk::get_gold(building) / 10000) * (造船_特技 ? 30 : 10);
			// 每一个相邻地方水军据点都加30分
			ratio += sea_enemy_count * 30;
			// 非特产城市,造太多也没用,过剩减分
			ratio += (理想船只 - 总船只) * 5;
		}

		// if (building.get_id() == 铃据点_江陵)
		// 	pk::trace(pk::format("造船ratio:{},", ratio));
		// 玩家的军团命令:重视舰船
		pk::district @district = pk::get_district(building.get_district_id());
		if (building.is_player() and !pk::is_player_controlled(district) and district.produce_kansen)
			ratio = int(ratio * 1.2);

		if (building.is_player() and !pk::is_player_controlled(district) and !district.produce_kansen)
			ratio = int(ratio * 0.3);

		ratio = pk::clamp(ratio, 0, 300); // 防止溢出,最大值为500.

		return ratio;
	}
	int AI筑城设置(pk::building @building)
	{
		// 小城市不筑城
		if (building.get_id() >= 城市_末)
			return 0;

		// 没钱不筑城
		if (pk::get_gold(building) < 3000 and !pk::enemies_around(building))
			return 0;

		int ratio = 100;
		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());

		if (pk::get_max_hp(building) - building.hp > 0)
			ratio = 300;

		if (pk::enemies_around(building))
			ratio = 300;
		if (building_p.city_defense < 8000)
			return ratio;
		else
			return 0;
	}
	float get_policy_buf(int policy)
	{
		float buf;
		switch (policy)
		{
			case 方针_中华统一:
				buf = 1.2f;
				break;
			case 方针_地方统一:
				buf = 1.1;
				break;
			case 方针_州统一:
				buf = 1.1f;
				break;
			case 方针_现状维持:
				buf = 1.0f;
				break;
			case 方针_吴越割据:
				buf = 1.0f;
				break;
			case 方针_巴蜀割据:
				buf = 1.0f;
				break;
			default:
				buf = 1.0f;
				break;
		}

		return buf;
	}

	// 根据规则获取武将
	pk::list<pk::person @> get_person_list(pk::building @base, int type, bool must_skill = false, bool check_role = false)
	{
		auto param = auto_inner::get_inner_type_stat_skill(type);
		int param1 = param.first;  // 排序规则1-能力
		int param2 = param.second; // 排序规则2-特技

		return sort_person_list(base, param1, param2, must_skill, check_role);
	}

	int cmd_stat;
	int cmd_skill;
	bool cmd_must_skill;
	pk::list<pk::person @> sort_person_list(pk::building @base, int stat, int skill, bool must_skill = false, bool check_role = false)
	{
		cmd_stat = stat;
		cmd_skill = skill;
		cmd_must_skill = must_skill;
		pk::list<pk::person @> actors;
		pk::list<pk::person @> select_list_person;
		pk::list<pk::person @> list_person = pk::get_person_list(base, pk::mibun_flags(身份_君主, 身份_都督, 身份_太守, 身份_一般));
		// 需要排除其他已经设定过的执行者
		if (check_role)
		{
			for (int i = 0; i < int(list_person.count); i += 1)
			{
				int person_id = list_person[i].get_id();
				//  if (base.get_id() == 铃据点_天水)
		     	// pk::trace(pk::format("城市id：{},人:{},武将角色:{}", base.get_id(), pk::decode(pk::get_name(list_person[i])), ch::get_person_p(person_id).auto_affairs_role));
				auto person_p = ch::get_person_p(person_id);
				if (list_person[i].service == int(person_p.auto_affairs_base) and ch::is_auto_affairs_role(person_p.auto_affairs_role))
				 	continue;
				select_list_person.add(list_person[i]);
			}
		}
		else
			select_list_person = list_person;

		if (select_list_person.count == 0)
			return actors;
		// if (调试模式)
		// pk::message_box(pk::encode(pk::format("排序前，目标数量不为0", 1)));

		select_list_person.sort(function(a, b) {
			if (cmd_skill >= 0 and cmd_must_skill)
			{
				bool a_skill = ch::has_skill(a, cmd_skill);
				bool b_skill = ch::has_skill(b, cmd_skill);

				return (a_skill ? (500 + a.stat[cmd_stat]) : 0) > (b_skill ? (500 + b.stat[cmd_stat]) : 0);
			}

			return a.stat[cmd_stat] > b.stat[cmd_stat];
		});
		if (cmd_must_skill)
		{
			if (ch::has_skill(select_list_person[0], skill))
				return select_list_person;
			return actors;
		}
		return select_list_person;
	}

	void 自动任命官员(pk::force @force, pk::building @building)
	{
		pk::person @taishu = pk::get_person(pk::get_taishu_id(building));
		if (pk::get_taishu_id(building) == -1)
		{
			pk::message_box(pk::encode("当前城池无太守"));
			return;
		} // 无太守
		if (taishu.service != building.get_id())
		{
			pk::message_box(pk::encode("太守不在城里"));
			return;
		} // 太守不在城里
		// auto district = pk::get_district(pk::get_district_id(force, 1));
		// if (district.ap < RANK_ACTION_COST)
		// {
		// 	pk::message_box(pk::encode(pk::format("军团行动力不足，需要\x1b[27x{}\x1b[0x行动力", RANK_ACTION_COST)));
		// 	return;
		// } // 军团行动力不足
		// if (force.tp < RANK_TP_COST)
		// {
		// 	pk::message_box(pk::encode(pk::format("技巧不足，需要\x1b[27x{}\x1b[0x技巧", RANK_TP_COST)));
		// 	return;
		// } // 技巧不足
		// if (pk::choose(pk::encode("要自动任命内政官吗?"), { pk::encode(" 是 "), pk::encode(" 否 ") }) == 1)
		// 	return;

		int building_id = building.get_id();
		auto base_t = ch::get_base_p(building_id);

		pk::building @base = pk::get_building(building_id);
		pk::list<pk::person @> person_list;

		auto building_p = ch::get_baseIA(base.get_id());
		array<uint8> rank_type = building_p.rank_type;

		for (int index = 0; index < int(rank_type.length); index += 1)
		{
			int type = rank_type[index];
			building_p.set_charge(-1, type);
			// 获取ai武将排序
			person_list = get_person_list(base, type);
			if (person_list.count > 0)
			{
				for (int p = 0; p < person_list.count; p += 1)
				{
					pk::person @person = person_list[p];
					if (index == 0)
					{
						building_p.set_charge(person.get_id(), type);
						break;
					}
					else
					{
						bool checkout_role = true;
						for (int rank = 0; rank < index; rank += 1)
						{
							checkout_role = (checkout_role and person.get_id() != building_p.get_charge(rank_type[rank]));
						}
						if (checkout_role)
						{
							building_p.set_charge(person.get_id(), type);
							// pk::trace(pk::format("城市id：{},城市名:{},人物ID:{},type:{}", building.get_id(),pk::decode(pk::get_name(building)),person.get_id(),building_p.well_gain,type));
							break;
						}
					}
				}
			}
		}

		pk::message_box(pk::encode("已完成自动内政官的任命。"), taishu);
		taishu.action_done == true;

		// pk::add_ap(district, -int(RANK_ACTION_COST));
		// pk::add_tp(force, -RANK_TP_COST, building.get_pos());
		// district.update();

		calculate_building2(building, false);
		// 信息显示_顶部面板();
	}

	// 征兵计算
	int calc_troops_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		auto base_t = ch::get_base_p(base.get_id());

		if (pk::is_valid_person_id(building_p.recruit_person))
		{
			pk::person @actor = pk::get_person(building_p.recruit_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.troops_gain = 0;
				building_p.set_charge(-1, 内政自动化_招兵);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);

			building_p.troops_gain = RECRUIT_TROOPS_CHANGE::get_recruit_num(base, actors, 1); // 计算
			 // pk::trace("building_p.recruit_person:" + building_p.recruit_person +","+ building_p.troops_gain);

			if (pk::get_max_troops(base) == base.troops)
			{
				building_p.troops_gain = 0;
				return 0;
			}

			building_p.troops_gain = pk::min(building_p.troops_gain, base_t.mil_pop_all);

			if (update_data)
			{
				building_p.troops_gain = RECRUIT_TROOPS_CHANGE::get_recruit_num(base, actors, 0);
				ch::add_troops(base, building_p.troops_gain, true);
				// pk::add_gold(base, -building_p.troops_effic, true);
				// 治安变化
				int p_order_change = RECRUIT_PUBLIC_ORDER_CHANGE::get_recruit_order_dec(base, actors, building_p.troops_gain);
				ch::add_public_order(base, p_order_change, true, false); // 治安函数已经处理法治，这里不用再次处理
				//pk::trace("p_order_change"+p_order_change+"城市" +base.get_id() );

				bool absent = pk::is_absent(actor);
				float ratio = building_p.troops_effic / 300.0 * 2 / (absent ? 2 : 1);
				pk::add_kouseki(actor, int(KOUSEKI_GAIN * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * ratio), base.get_pos());

#if param_set
				pk::add_stat_exp(actor, pk::get_recruit_param().stat, int(2 * ratio));
#endif
#if not_param_set
				pk::add_stat_exp(actor, int(pk::core["recruit.stat"]), int(2 * ratio));
#endif

				base.update();
				return building_p.troops_effic;
			}
		}
		else
			building_p.troops_gain = 0;

		return 0;
	}

	// 巡查计算
	int calc_porder_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.inspections_person))
		{
			pk::person @actor = pk::get_person(building_p.inspections_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.porder_gain = 0;
				building_p.set_charge(-1, 内政自动化_巡查);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			building_p.porder_gain = INSPECTIONS::get_inspections_order_inc(base, actors);

			if (update_data)
			{
				if (base.get_id() >= 建筑_城市末)
				{
					auto base_t = ch::get_base_p(base.get_id());
					if (int(base_t.public_order) == 100) return 0;
						
				}
				else
				{
					pk::city @city = pk::building_to_city(base);
					if (city.public_order == 100) return 0;
				}
				ch::add_public_order(base, building_p.porder_gain, true);
				// pk::add_gold(base, -building_p.porder_effic, true);


				float ratio = building_p.porder_effic / 100.0 * 2;

				pk::add_kouseki(actor, int(KOUSEKI_GAIN * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * ratio), base.get_pos());

#if param_set
				pk::add_stat_exp(actor, pk::get_patrol_param().stat, int(2 * ratio));
#endif
#if not_param_set
				pk::add_stat_exp(actor, int(pk::core["inspection.stat"]), int(2 * ratio));
#endif
				return building_p.porder_effic;
			}
		}
		else
			building_p.porder_gain = 0;

		return 0;
	}

	// 训练计算
	int calc_train_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.drill_person))
		{
			pk::person @actor = pk::get_person(building_p.drill_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.train_gain = 0;
				building_p.set_charge(-1, 内政自动化_训练);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			building_p.train_gain = DRILL::get_drill_energy_change(base, actors);

			if (update_data)
			{
				if (pk::get_energy(base) == pk::get_max_energy(base)) return 0;
					
				pk::add_energy(base, building_p.train_gain, true);
				// pk::add_gold(base, -building_p.train_effic, true);

				bool absent = pk::is_absent(actor);
				float ratio = building_p.train_effic / 100.0 * 2 / (absent ? 2 : 1);

				pk::add_kouseki(actor, int(KOUSEKI_GAIN * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * ratio), base.get_pos());

#if param_set
				pk::add_stat_exp(actor, pk::get_drill_param().stat, int(2 * ratio));
#endif
#if not_param_set
				pk::add_stat_exp(actor, int(pk::core["train.stat"]), int(2 * ratio));
#endif
				return building_p.train_effic;
			}
		}
		else
			building_p.train_gain = 0;

		return 0;
	}

	// 兵装计算
	int calc_weapon_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.weapon_person))
		{
			pk::person @actor = pk::get_person(building_p.weapon_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.bow_gain = 0;
				building_p.spear_gain = 0;
				building_p.halberd_gain = 0;
				building_p.set_charge(-1, 内政自动化_兵装);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			int spear_gain = PRODUCE::cal_produce_gain(base, actors, 兵器_枪);
			int halberd_gain = PRODUCE::cal_produce_gain(base, actors, 兵器_戟);
			int bow_gain = PRODUCE::cal_produce_gain(base, actors, 兵器_弩);

			if (pk::enemies_around(base))
			{
				spear_gain = spear_gain / 3;
				halberd_gain = halberd_gain / 3;
				bow_gain = bow_gain / 3;
			}
			// pk::info("系数： " + float(pk::core["tokusan.sale_rate"]));
			bool 特色_枪 = false;
			bool 特色_戟 = false;
			bool 特色_弩 = false;
			if (base.get_id() < 42) // 城市判定
			{
				pk::city @city = pk::building_to_city(base);
				特色_枪 = city.tokusan[pk::equipment_id_to_heishu(兵器_枪)];
				特色_戟 = city.tokusan[pk::equipment_id_to_heishu(兵器_戟)];
				特色_弩 = city.tokusan[pk::equipment_id_to_heishu(兵器_弩)];
			}

			float scale = 0.0f;
			bool weapon_枪_max = pk::get_max_weapon_amount(base, 兵器_枪) == pk::get_weapon_amount(base, 兵器_枪);
			bool weapon_戟_max = pk::get_max_weapon_amount(base, 兵器_戟) == pk::get_weapon_amount(base, 兵器_戟);
			bool weapon_弩_max = pk::get_max_weapon_amount(base, 兵器_弩) == pk::get_weapon_amount(base, 兵器_弩);

			if (building_p.weapon_choose == 0) // 均衡
			{
				building_p.spear_gain = int(spear_gain / 3 * (特色_枪 ? tokusan_sale : 1));
				building_p.halberd_gain = int(halberd_gain / 3 * (特色_戟 ? tokusan_sale : 1));
				building_p.bow_gain = int(bow_gain / 3 * (特色_弩 ? tokusan_sale : 1));
				scale = (3 - (weapon_枪_max ? 1 : 0) - (weapon_戟_max ? 1 : 0) - (weapon_弩_max ? 1 : 0)) / 3.f;
			}
			if (building_p.weapon_choose == 1) // 重视枪
			{
				building_p.spear_gain = int(spear_gain * 1.0 * (特色_枪 ? tokusan_sale : 1));
				building_p.halberd_gain = int(halberd_gain * 0 * (特色_戟 ? tokusan_sale : 1));
				building_p.bow_gain = int(bow_gain * 0 * (特色_弩 ? tokusan_sale : 1));
				scale = weapon_枪_max ? 0.f : 1.f;
			}
			if (building_p.weapon_choose == 2) // 重视戟
			{
				building_p.spear_gain = int(spear_gain * 0 * (特色_枪 ? tokusan_sale : 1));
				building_p.halberd_gain = int(halberd_gain * 1 * (特色_戟 ? tokusan_sale : 1));
				building_p.bow_gain = int(bow_gain * 0 * (特色_弩 ? tokusan_sale : 1));
				scale = weapon_戟_max ? 0.f : 1.f;
			}
			if (building_p.weapon_choose == 3) // 重视弩
			{
				building_p.spear_gain = int(spear_gain * 0 * (特色_枪 ? tokusan_sale : 1));
				building_p.halberd_gain = int(halberd_gain * 0 * (特色_戟 ? tokusan_sale : 1));
				building_p.bow_gain = int(bow_gain * 1 * (特色_弩 ? tokusan_sale : 1));
				scale = weapon_弩_max ? 0.f : 1.f;
			}
			if (update_data)
			{
				//pk::info(pk::format("building:{}; scale: {}; gold: {}", pk::decode(pk::get_name(base)), scale, int(building_p.weapon_effic * scale)));
				pk::add_weapon_amount(base, 兵器_枪, building_p.spear_gain, true);
				pk::add_weapon_amount(base, 兵器_戟, building_p.halberd_gain, true);
				pk::add_weapon_amount(base, 兵器_弩, building_p.bow_gain, true);
				// pk::add_gold(base, -int(building_p.weapon_effic * scale), true);

				bool absent = pk::is_absent(actor);
				float ratio = building_p.weapon_effic / 700.0 * 2 / (absent ? 2 : 1);
				pk::add_kouseki(actor, int(KOUSEKI_GAIN * scale * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * scale * ratio), base.get_pos());

#if param_set
				if (int(scale) > 0)
					pk::add_stat_exp(actor, pk::get_produce_param().stat, int(3 * scale * ratio));
#endif
#if not_param_set
				if (int(scale) > 0)
					pk::add_stat_exp(actor, int(pk::core["weapon_produce.stat"]), int(3 * scale * ratio));
#endif
				return int(building_p.weapon_effic * scale);
			}
		}
		else
		{
			building_p.bow_gain = 0;
			building_p.spear_gain = 0;
			building_p.halberd_gain = 0;
		}

		return 0;
	}

	// 育马
	int calc_horse_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.horse_person))
		{
			pk::person @actor = pk::get_person(building_p.horse_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.horse_gain = 0;
				building_p.set_charge(-1, 内政自动化_育马);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			bool 特色_马 = false;
			if (base.get_id() < 42) // 城市判定
			{
				pk::city @city = pk::building_to_city(base);
				特色_马 = city.tokusan[pk::equipment_id_to_heishu(兵器_战马)];
			}
			int horse_gain = PRODUCE::cal_produce_gain(base, actors, 兵器_战马);
			building_p.horse_gain = int(horse_gain * (特色_马 ? tokusan_sale : 1));

			if (pk::enemies_around(base))
				building_p.horse_gain = building_p.horse_gain / 2;

			if (update_data)
			{
				if (pk::get_max_weapon_amount(base, 兵器_战马) == pk::get_weapon_amount(base, 兵器_战马)) return 0;
				pk::add_weapon_amount(base, 兵器_战马, building_p.horse_gain, true);
				// pk::add_gold(base, -building_p.horse_effic, true);

				bool absent = pk::is_absent(actor);
				float ratio = int(building_p.horse_effic / 300.0 * 2 / (absent ? 2 : 1));

				pk::add_kouseki(actor, int(KOUSEKI_GAIN * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * ratio), base.get_pos());

#if param_set
				pk::add_stat_exp(actor, pk::get_produce_param().stat, int(2 * ratio));
#endif
#if not_param_set
				pk::add_stat_exp(actor, int(pk::core["weapon_produce.stat"]), int(2 * ratio));
#endif
				return building_p.horse_effic;
			}
		}
		else
			building_p.horse_gain = 0;

		return 0;
	}

	// 攻具计算
	int calc_punch_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.punch_person))
		{
			pk::person @actor = pk::get_person(building_p.punch_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.well_gain = 0;
				building_p.punch_gain = 0;
				building_p.set_charge(-1, 内政自动化_攻具);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			bool 特色_冲车 = false;
			if (base.get_id() < 42) // 城市判定
			{
				pk::city @city = pk::building_to_city(base);
				特色_冲车 = city.tokusan[pk::equipment_id_to_heishu(兵器_冲车)];
			}

			int day_inc = PRODUCE_TIME_COST::get_produce_time(actors, 兵器_冲车);

			day_inc = int(day_inc * (特色_冲车 ? tokusan_sale : 1));

			day_inc = pk::min(day_inc, 1000);

			building_p.punch_gain = 0;
			building_p.well_gain = 0;

			float scale = 0.0f;
			int weapon_well = pk::has_tech(base, 技巧_投石开发) ? 兵器_投石 : 兵器_井阑;
			int weapon_punch = pk::has_tech(base, 技巧_木兽开发) ? 兵器_木兽 : 兵器_冲车;

			bool weapon_well_max = pk::get_max_weapon_amount(base, weapon_well) == pk::get_weapon_amount(base, weapon_well);
			bool weapon_punch_max = pk::get_max_weapon_amount(base, weapon_punch) == pk::get_weapon_amount(base, weapon_punch);
			switch (building_p.arms_choose)
			{
				case 0:
					building_p.well_gain = int(day_inc / 2);
					building_p.punch_gain = int(day_inc / 2);
					scale = (2 - (weapon_well_max ? 1 : 0) - (weapon_punch_max ? 1 : 0)) / 2.f;
					break;
				case 1:
					building_p.well_gain = int(day_inc);
					scale = weapon_well_max ? 0.f : 1.f;
					break;
				case 2:
					building_p.punch_gain = int(day_inc);
					scale = weapon_punch_max ? 0.f : 1.f;
					break;
				default:
					break;
			}
			if (update_data)
			{
				if (!weapon_well_max)
				{
					building_p.well += building_p.well_gain;
					if (building_p.well > 1000)
					{
						pk::add_weapon_amount(base, weapon_well, 1, true);
						building_p.well -= 1000;
					}
				}

				if (!weapon_punch_max)
				{
					building_p.punch += building_p.punch_gain;
					if (building_p.punch > 1000)
					{
						pk::add_weapon_amount(base, weapon_punch, 1, true);
						building_p.punch -= 1000;
					}
				}

				// pk::add_gold(base, -int(building_p.punch_effic * scale), true);

				bool absent = pk::is_absent(actor);
				float ratio = building_p.punch_effic / 300.0 * 2 / (absent ? 2 : 1);

				pk::add_kouseki(actor, int(KOUSEKI_GAIN * scale * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * scale * ratio), base.get_pos());

#if param_set
				if (int(scale) > 0)
					pk::add_stat_exp(actor, pk::get_produce_param().stat, int(2 * scale * ratio));
#endif
#if not_param_set
				if (int(scale) > 0)
					pk::add_stat_exp(actor, int(pk::core["weapon_produce.stat"]), int(2 * scale * ratio));
#endif
				return int(building_p.punch_effic * scale);
			}
		}
		else
		{
			building_p.well_gain = 0;
			building_p.punch_gain = 0;
		}
		return 0;
	}

	// 船舰计算
	int calc_boat_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (pk::is_valid_person_id(building_p.boat_person))
		{
			pk::person @actor = pk::get_person(building_p.boat_person);
			if (!pk::is_alive(actor) or actor.get_force_id() != base.get_force_id())
			{
				building_p.boat_gain = 0;
				building_p.set_charge(-1, 内政自动化_造船);
			}
			pk::list<pk::person @> actors;
			actors.add(actor);
			bool 兵种_水军 = false;
			if (base.get_id() < 42) // 城市判定
			{
				pk::city @city = pk::building_to_city(base);
				兵种_水军 = city.tokusan[pk::equipment_id_to_heishu(兵器_斗舰)];
			}

			int day_inc = PRODUCE_TIME_COST::get_produce_time(actors, 兵器_楼船);

			day_inc = int(day_inc * (兵种_水军 ? tokusan_sale : 1));

			day_inc = pk::min(day_inc, 1000);

			building_p.boat_gain = day_inc;

			// building_p.boat_gain = pk::min(building_p.boat_gain, 1000);

			if (update_data)
			{
				int weapon_id = pk::has_tech(base, 技巧_投石开发) ? 兵器_斗舰 : 兵器_楼船;
				if (pk::get_max_weapon_amount(base, weapon_id) == pk::get_weapon_amount(base, weapon_id)) return 0;
					
				building_p.boat += building_p.boat_gain;
				if (building_p.boat > 1000)
				{
					pk::add_weapon_amount(base, weapon_id, 1, true);
					building_p.boat -= 1000;
				}
				// pk::add_gold(base, -building_p.boat_effic, true);

				bool absent = pk::is_absent(actor);
				float ratio = building_p.boat_effic / 200.0 * 2 / (absent ? 2 : 1);
				pk::add_kouseki(actor, int(KOUSEKI_GAIN * ratio));
				pk::add_tp(pk::get_force(base.get_force_id()), int(TP_GAIN * ratio), base.get_pos());

#if param_set
				pk::add_stat_exp(actor, pk::get_produce_param().stat, int(2 * ratio));
#endif
#if not_param_set
				pk::add_stat_exp(actor, int(pk::core["weapon_produce.stat"]), int(2 * ratio));
#endif
				return building_p.boat_effic;
			}
		}
		else
			building_p.boat_gain = 0;

		return 0;
	}

	// 城防计算
	int calc_repair_gain(pk::building @base, bool update_data = false)
	{
		auto building_p = ch::get_baseIA(base.get_id());
		if (building_p.repair_effic > 0)
		{
			building_p.repair_gain = building_repair(base);

			// if (base.get_id() == 铃据点_零陵)
			// 	pk::trace(pk::format("城市id：{},城市名:{},building_p.repair_gain:{},building_p.repair_effic:{}", base.get_id(), pk::decode(pk::get_name(base)), building_p.repair_gain,building_p.repair_effic));

			// 难度修正
			building_p.repair_gain = int(building_p.repair_gain * (base.is_player() ? 100 : inter::domestic_difficulty_impact()) / 100.f);

			// 玩家电脑区别对待
			building_p.repair_gain = int(building_p.repair_gain * (base.is_player() ? 玩家生产倍率 : 电脑生产倍率) / 100.f);

			if (pk::get_taishu_id(base) == -1) return 0;
				
			pk::person @taishu = pk::get_person(pk::get_taishu_id(base));
			if (update_data and building_p.repair_gain > 0 and pk::is_in_screen(base.pos) and pk::enemies_around(base))
				pk::say(pk::encode("大家在敌人攻过来之前,快进行维修!"), taishu);

			if (update_data)
			{
				// 城防系统
				int effic_gold = 0;
				if (building_p.city_defense - pk::get_max_hp(base) < 0)
				{
					if (int(pk::get_gold(base)) - building_p.repair_effic > 0)
					{
						if (base.hp + building_p.city_defense < 15000)
						{
							building_p.city_defense += int(building_p.repair_gain);
							effic_gold = building_p.repair_effic;
							// pk::add_gold(base, -building_p.repair_effic, true);
						}
					}
				}
				building_p.city_defense = pk::clamp(building_p.city_defense, 0, pk::get_max_hp(base)); // 防止溢出,额外城防最大为原始最大城防.
				building_p.city_defense = pk::min(building_p.city_defense, 5000);

				if ((int(base.hp) - int(pk::get_max_hp(base)) < 0))
				{
					int 可修复防御 = pk::get_max_hp(base) - base.hp;
					int 修复防御 = pk::min(可修复防御, building_p.city_defense);
					if (building_p.city_defense - 可修复防御 >= 0)
					{
						building_p.city_defense -= 修复防御;
						pk::add_hp(base, 修复防御, true);
					}
					else
					{
						building_p.city_defense = 0;
						pk::add_hp(base, building_p.city_defense, true);
					}

					base.hp = pk::min(base.hp, pk::get_max_hp(base));
				}
				// 最高为8000

				base.update();
				return effic_gold;
			}
		}
		else
			building_p.repair_gain = 0;

		return 0;
	}

	int building_repair(pk::building @building)
	{
		if (building.get_force_id() == -1)
			return 0;
		auto base_t = ch::get_base_p(building.get_id());
		auto building_p = ch::get_baseIA(building.get_id());
		pk::force @force = pk::get_force(building.get_force_id());
		pk::person @taishu = pk::get_person(pk::get_taishu_id(building));

		float 势力方针修正 = 1.0;

		// 初始值赋值
		float 筑城效率 = 1.0;

		int repair_base_gain = int((building_p.repair_effic / 3));

		// 城市人口系统和之招兵系统/生产系统绑定,底子依然是人口数量,生产能力也基于人口数量
		// 通过城市里面太守和普通人的加总双属性增加内政效果.

		if (pk::get_taishu_id(building) != -1 and building.get_id() >= 0 and building.get_id() < 87)
		{

			auto ilban_list = pk::list_to_array(pk::get_person_list(building, pk::mibun_flags(身份_一般)));
			if (0 < ilban_list.length)
			{
				for (int i = 0; i < int(ilban_list.length); i++)
				{
					pk::person @ilban = ilban_list[i];
					if (!pk::is_unitize(ilban) and !pk::is_absent(ilban))

						筑城效率 += float(pk::max(ilban.stat[武将能力_政治] + ilban.stat[武将能力_武力] - 50, 0)) * 0.02 * 0.05 + 0.02;
					// pk::trace("temp" + ilban.service);
				}
			}
		}

		// if (building.get_id() == 铃据点_零陵)
		// 	pk::trace(pk::format("城市id：{},城市名:{},筑城效率:{}", building.get_id(), pk::decode(pk::get_name(building)), 筑城效率));

		筑城效率 = pk::min(筑城效率, 1.5);

		// 筑城可以突破上限的增加0.5
		int person_id = building.who_has_skill(特技_筑城);
		if (pk::is_valid_person_id(person_id))
		{

			筑城效率 += 0.5;
		}

		return int(repair_base_gain * 筑城效率);
	}
	void AI内政官任命(pk::building @building)
	{
		pk::list<pk::person @> person_list; // = pk::get_person_list(pk::get_building(city_id), pk::mibun_flags(身份_君主, 身份_都督, 身份_太守, 身份_一般));

		auto building_p = ch::get_baseIA(building.get_id());
		array<uint8> rank_type = building_p.rank_type;
		//pk::trace(pk::format("城市id：{},城市名:{},任命:{}", building.get_id(), pk::decode(pk::get_name(building)), int(rank_type.length)));

		for (int index = 0; index < int(rank_type.length); index += 1)
		{
			int type = rank_type[index];
			// 获取ai武将排序
			building_p.set_charge(-1, type);
			// 排特技
			person_list = get_person_list(building, type, true, true);
			if (person_list.count > 0)
			{
				building_p.set_charge(person_list[0].get_id(), type);
				//pk::trace(pk::format("城市id：{},城市名:{},任命:{}", building.get_id(), pk::decode(pk::get_name(building)), pk::decode(pk::get_name(person_list[0]))));
			}
		}

		for (int index = 0; index < int(rank_type.length); index += 1)
		{
			int type = rank_type[index];
			if (pk::is_valid_person_id(building_p.get_charge(type)))
				continue;
			person_list = get_person_list(building, type, false, true);
			if (person_list.count > 0)
			{
				building_p.set_charge(person_list[0].get_id(), type);
			}
		}

		for (int index = 0; index < int(rank_type.length); index += 1)
		{
			int type = rank_type[index];
			if (pk::is_valid_person_id(building_p.get_charge(type)))
				continue;
			building_p.set_effic(0, type);
		}
	}

	int 开支级别(pk::building @building, int 内政自动化_项目)
	{
		int base_id = building.get_id();
		int level = ch::get_city_level(base_id);
		array<int> cost_list;
		switch (内政自动化_项目)
		{
			case 内政自动化_巡查:
				cost_list = {/*0*/ 100, /*1*/ 100, /*2*/ 100, /*3*/ 150, /*4*/ 200 };
				break;
			case 内政自动化_训练:
				cost_list = {/*0*/ 100, /*1*/ 100, /*2*/ 100, /*3*/ 150, /*4*/ 200 };
				break;
			case 内政自动化_招兵:
				cost_list = {/*0*/ 300, /*1*/ 300, /*2*/ 300, /*3*/ 300, /*4*/ 300 };
				break;
			case 内政自动化_兵装:
				cost_list = {/*0*/ 700, /*1*/ 700, /*2*/ 700, /*3*/ 800, /*4*/ 900 };
				break;
			case 内政自动化_育马:
				cost_list = {/*0*/ 700, /*1*/ 700, /*2*/ 700, /*3*/ 800, /*4*/ 900 };
				break;
			case 内政自动化_攻具:
				cost_list = {/*0*/ 300, /*1*/ 300, /*2*/ 300, /*3*/ 400, /*4*/ 500 };
				break;
			case 内政自动化_造船:
				cost_list = {/*0*/ 200, /*1*/ 200, /*2*/ 200, /*3*/ 300, /*4*/ 300 };
				break;
			case 内政自动化_筑城:
				cost_list = {/*0*/ 100, /*1*/ 100, /*2*/ 150, /*3*/ 150, /*4*/ 200 };
				break;
		}
		return cost_list[level];
	}

	string numberpad_t(int line, int original_value, int current_value)
	{
		return pk::encode("");
	}

	void 重置内政官(pk::building @building)
	{
		int building_id = building.get_id();
		auto building_p = ch::get_baseIA(building_id);

		if ((ch::get_auto_affairs_status(building) or 调试模式))
		{
			array<uint8> rank_type = building_p.rank_type;
			for (int type = 0; type < int(rank_type.length); type += 1)
			{
				int person_id = building_p.get_charge(rank_type[type]);
				if (!pk::is_valid_person_id(person_id))
					continue;

				pk::person @person = pk::get_person(person_id);
				int force_id = person.get_force_id();

				if (!pk::is_normal_force(force_id) or (building.is_player() and person.order == 武将任务_移动) or person.service != building_id or !pk::check_mibun(person, pk::mibun_flags(身份_一般, 身份_太守, 身份_都督, 身份_君主)))
				{
					if (building.is_player() and person.get_force_id() == building.get_force_id())
						pk::history_log(building.pos, pk::get_force(building.get_force_id()).color, pk::encode(pk::format("{}的\x1b[1x{}\x1b[16x{}\x1b[1x被取消\x1b[1x", pk::decode(pk::get_name(building)), building_p.get_charge_name(rank_type[type]), pk::decode(pk::get_name(person)))));
					building_p.set_charge(-1, rank_type[type]);

					switch (rank_type[type])
					{
						case 内政自动化_招兵:
							building_p.troops_effic = 0;
							break;
						case 内政自动化_训练:
							building_p.train_effic = 0;
							break;
						case 内政自动化_巡查:
							building_p.porder_effic = 0;
							break;
						case 内政自动化_兵装:
							building_p.weapon_effic = 0;
							break;
						case 内政自动化_育马:
							building_p.horse_effic = 0;
							break;
						case 内政自动化_攻具:
							building_p.punch_effic = 0;
							break;
						case 内政自动化_造船:
							building_p.boat_effic = 0;
							break;
					}
				}
			}
		}
	}
}
// namespace
